package com.jz.digital.dialog

import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import com.jz.digital.R
import com.jz.digital.base.BaseDialogFragment
import com.jz.digital.utils.DipPixelUtil
import kotlinx.android.synthetic.main.dialog_white_confirm.rtv_title
import kotlinx.android.synthetic.main.dialog_white_confirm.tv_cancel
import kotlinx.android.synthetic.main.dialog_white_confirm.tv_confirm
import kotlinx.android.synthetic.main.dialog_white_confirm.tv_message

/**
 * Copyright (c) 2019 深圳市爱都科技有限公司. All rights reserved.
 *
 * @Author: xyb
 * @CreateDate: 2020-04-12 18:20
 * @description
 */
class CommBottomConfirmDialog : BaseDialogFragment() {

    private var mConfirmListener: View.OnClickListener? = null
    private var mCancelListener: View.OnClickListener? = null
    private var mDismissWithConfirm = false
    private var mDismissWithCancel = false

    override fun getLayoutResId(): Int {
        return R.layout.dialog_white_confirm
    }

    override fun initData() {
        val bundle = arguments
        if (bundle != null) {
            val title = bundle.getString(TITLE)
            if (TextUtils.isEmpty(title)) {
                rtv_title!!.visibility = View.GONE
            } else {
                rtv_title!!.text = title
            }
            tv_message!!.text = bundle.getString(MESSAGE)
            tv_confirm!!.text = bundle.getString(TEXT_CONFIRM)
            tv_cancel!!.text = bundle.getString(TEXT_CANCEL)
            tv_cancel!!.visibility =
                if (bundle.getBoolean(SHOW_CANCEL)) View.VISIBLE else View.GONE
            mDismissWithConfirm = bundle.getBoolean(DISMISS_WHIT_CONFIRM, true)
            mDismissWithCancel = bundle.getBoolean(DISMISS_WHIT_CANCEL, true)
        }
    }

    fun setOnConfirmListener(listener: View.OnClickListener?): CommBottomConfirmDialog {
        mConfirmListener = listener
        return this
    }

    fun setCancelListener(listener: View.OnClickListener?): CommBottomConfirmDialog {
        mCancelListener = listener
        return this
    }

    override fun initUI(view: View) {
        super.initUI(view)
        if (dialog != null && dialog!!.window != null) {
            val window = dialog!!.window
            window!!.decorView.setPadding(
                DipPixelUtil.dip2px(10f),
                0,
                DipPixelUtil.dip2px(10f),
                DipPixelUtil.dip2px(15f)
            ) // 宽度持平
            val lp = window.attributes
            lp.gravity = Gravity.BOTTOM // 紧贴底部
            lp.width = WindowManager.LayoutParams.MATCH_PARENT // 宽度持平
            window.attributes = lp
        }
        tv_cancel?.setOnClickListener {
            if (mDismissWithCancel) {
                this.dismissAllowingStateLoss()
            }
            if (mCancelListener != null) {
                mCancelListener!!.onClick(it)
            }
        }
        tv_confirm?.setOnClickListener {
            if (mDismissWithConfirm) {
                this.dismissAllowingStateLoss()
            }
            if (mConfirmListener != null) {
                mConfirmListener!!.onClick(it)
            }
        }
    }

    /**
     * 初始化主题颜色
     */
    private fun initThemeColor() {
    }

    override fun onResume() {
        super.onResume()
        //setWindowSize();
    }

    /**
     * 设置窗口大小
     */
    private fun setWindowSize() {
        if (dialog!!.window != null) {
            dialog!!.window!!.setLayout(
                DipPixelUtil.dip2px(296f),
                DipPixelUtil.dip2px(210f)
            )
        }
    }

    companion object {
        private const val TITLE = "title"
        private const val MESSAGE = "message"
        private const val TEXT_CONFIRM = "textConfirm"
        private const val TEXT_CANCEL = "textCancel"
        private const val SHOW_CANCEL = "showCancel"
        private const val DISMISS_WHIT_CONFIRM = "dismiss_whit_confirm"
        private const val DISMISS_WHIT_CANCEL = "dismiss_whit_cancel"

        /**
         * 主按钮颜色
         */
        private const val KEY_MAIN_TEXT_COLOR = "main_text_color"

        @JvmStatic
        fun newInstance(
            message: String?, textConfirm: String?, textCancel: String?,
            showCancel: Boolean
        ): CommBottomConfirmDialog {
            val arguments = Bundle()
            arguments.putString(MESSAGE, message)
            arguments.putString(TEXT_CONFIRM, textConfirm)
            arguments.putString(TEXT_CANCEL, textCancel)
            arguments.putBoolean(SHOW_CANCEL, showCancel)
            val dialog = CommBottomConfirmDialog()
            dialog.arguments = arguments
            dialog.setStyle(STYLE_NO_TITLE, R.style.AlertDialog_Dark)
            return dialog
        }

        @JvmStatic
        fun newInstance(
            message: String?, textConfirm: String?, textCancel: String?,
            showCancel: Boolean, mainTextColor: Int
        ): CommBottomConfirmDialog {
            val arguments = Bundle()
            arguments.putString(MESSAGE, message)
            arguments.putString(TEXT_CONFIRM, textConfirm)
            arguments.putString(TEXT_CANCEL, textCancel)
            arguments.putBoolean(SHOW_CANCEL, showCancel)
            arguments.putInt(KEY_MAIN_TEXT_COLOR, mainTextColor)
            val dialog = CommBottomConfirmDialog()
            dialog.arguments = arguments
            dialog.setStyle(STYLE_NO_TITLE, R.style.AlertDialog_Dark)
            return dialog
        }

        @JvmStatic

        fun newInstance(
            title: String?, message: String?, textConfirm: String?, textCancel: String?,
            showCancel: Boolean
        ): CommBottomConfirmDialog {
            val arguments = Bundle()
            arguments.putString(TITLE, title)
            arguments.putString(MESSAGE, message)
            arguments.putString(TEXT_CONFIRM, textConfirm)
            arguments.putString(TEXT_CANCEL, textCancel)
            arguments.putBoolean(SHOW_CANCEL, showCancel)
            val dialog = CommBottomConfirmDialog()
            dialog.arguments = arguments
            dialog.setStyle(STYLE_NO_TITLE, R.style.AlertDialog_Dark)
            return dialog
        }

        /**
         *
         * @param title
         * @param message
         * @param textConfirm
         * @param textCancel
         * @param showCancel
         * @param mainTextColor 主按钮颜色
         * @return
         */
        @JvmStatic
        fun newInstance(
            title: String?, message: String?, textConfirm: String?, textCancel: String?,
            showCancel: Boolean, mainTextColor: Int
        ): CommBottomConfirmDialog {
            val arguments = Bundle()
            arguments.putString(TITLE, title)
            arguments.putString(MESSAGE, message)
            arguments.putString(TEXT_CONFIRM, textConfirm)
            arguments.putString(TEXT_CANCEL, textCancel)
            arguments.putBoolean(SHOW_CANCEL, showCancel)
            arguments.putInt(KEY_MAIN_TEXT_COLOR, mainTextColor)
            val dialog = CommBottomConfirmDialog()
            dialog.arguments = arguments
            dialog.setStyle(STYLE_NO_TITLE, R.style.AlertDialog_Dark)
            return dialog
        }

        @JvmStatic

        fun newInstance(
            title: String?, message: String?, textConfirm: String?, textCancel: String?,
            showCancel: Boolean, cancelable: Boolean
        ): CommBottomConfirmDialog {
            val arguments = Bundle()
            arguments.putString(TITLE, title)
            arguments.putString(MESSAGE, message)
            arguments.putString(TEXT_CONFIRM, textConfirm)
            arguments.putString(TEXT_CANCEL, textCancel)
            arguments.putBoolean(SHOW_CANCEL, showCancel)
            val dialog = CommBottomConfirmDialog()
            dialog.arguments = arguments
            dialog.isCancelable = cancelable
            dialog.setStyle(STYLE_NO_TITLE, R.style.AlertDialog_Dark)
            return dialog
        }

        @JvmStatic

        fun newInstance(
            title: String?, message: String?, textConfirm: String?, textCancel: String?,
            showCancel: Boolean, cancelable: Boolean, dismissWithConfirm: Boolean
        ): CommBottomConfirmDialog {
            val arguments = Bundle()
            arguments.putString(TITLE, title)
            arguments.putString(MESSAGE, message)
            arguments.putString(TEXT_CONFIRM, textConfirm)
            arguments.putString(TEXT_CANCEL, textCancel)
            arguments.putBoolean(SHOW_CANCEL, showCancel)
            arguments.putBoolean(DISMISS_WHIT_CONFIRM, dismissWithConfirm)
            val dialog = CommBottomConfirmDialog()
            dialog.arguments = arguments
            dialog.isCancelable = cancelable
            dialog.setStyle(STYLE_NO_TITLE, R.style.AlertDialog_Dark)
            return dialog
        }

        @JvmStatic

        fun newInstance(
            title: String?,
            message: String?,
            textConfirm: String?,
            textCancel: String?,
            showCancel: Boolean,
            cancelable: Boolean,
            dismissWithConfirm: Boolean,
            dismissWithCancel: Boolean
        ): CommBottomConfirmDialog {
            val arguments = Bundle()
            arguments.putString(TITLE, title)
            arguments.putString(MESSAGE, message)
            arguments.putString(TEXT_CONFIRM, textConfirm)
            arguments.putString(TEXT_CANCEL, textCancel)
            arguments.putBoolean(SHOW_CANCEL, showCancel)
            arguments.putBoolean(DISMISS_WHIT_CONFIRM, dismissWithConfirm)
            arguments.putBoolean(DISMISS_WHIT_CANCEL, dismissWithCancel)
            val dialog = CommBottomConfirmDialog()
            dialog.arguments = arguments
            dialog.isCancelable = cancelable
            dialog.setStyle(STYLE_NO_TITLE, R.style.AlertDialog_Dark)
            return dialog
        }
    }
}
